package com.machine.filling.db.dao;

import android.provider.Settings;

import com.machine.filling.MyApp;
import com.machine.filling.Test.TestCommon;
import com.machine.filling.db.greendao.ClientInfoModleDao;
import com.machine.filling.db.modle.ClientInfoModle;

/**
 * Created by Administrator on 2018/2/26 0026.
 * ClientInfo 只允许有一个实例，所以数据库也只存在一个
 */

public class ClientInfoDao {
    private static ClientInfoDao instance;
    private static ClientInfoModleDao clientDao;
    private static ClientInfoModle clientInfoModle;
    private final String deviceNum;

    private ClientInfoDao() {
        deviceNum = Settings.System.getString(MyApp.getInstance().getContentResolver(), Settings.Secure.ANDROID_ID);
    }

    public static ClientInfoDao getInstance() {
        if (instance == null) {
            instance = new ClientInfoDao();
            clientDao = MyApp.getInstance().getDaoSession().getClientInfoModleDao();
            clientInfoModle = new ClientInfoModle();
        }
        return instance;
    }

    public String getDeviceNum() {
        return deviceNum;
    }

    public ClientInfoModle getModleByDeviceNum() {
        ClientInfoModle modle = clientDao.queryBuilder().where(ClientInfoModleDao.Properties.DeviceNum
                .eq(deviceNum)).unique();
        if (modle == null) {
            clientInfoModle.setDeviceNum(deviceNum);
            clientDao.insert(clientInfoModle);
            modle = clientInfoModle;
        }
        return modle;
    }

    public void deleteByDeviceNum() {
        clientDao.delete(getModleByDeviceNum());
    }

    public void createDBByDeviceNum() {
        if (getModleByDeviceNum() == null) {
            clientInfoModle.setDeviceNum(deviceNum);
            clientDao.insert(clientInfoModle);
        }
    }

    public void updataToken(String token) {
        clientInfoModle = getModleByDeviceNum();
        clientInfoModle.setToken(token);
        clientDao.update(clientInfoModle);

    }

    public void updataDeviceId(String deviceId) {
        clientInfoModle = getModleByDeviceNum();
        clientInfoModle.setDeviceId(deviceId);
        clientDao.update(clientInfoModle);

    }

    public void updataStationId(String stationId) {
        clientInfoModle = getModleByDeviceNum();
        clientInfoModle.setStationId(stationId);
        clientDao.update(clientInfoModle);

    }

    public void updataStationName(String stationName) {
        clientInfoModle = getModleByDeviceNum();
        clientInfoModle.setStationName(stationName);
        clientDao.update(clientInfoModle);

    }

    public void updataStationAddr(String stationAddr) {
        clientInfoModle = getModleByDeviceNum();
        clientInfoModle.setStationAddr(stationAddr);
        clientDao.update(clientInfoModle);

    }

    public void updataStationNum(String stationNum) {
        clientInfoModle = getModleByDeviceNum();
        clientInfoModle.setStationNum(stationNum);
        clientDao.update(clientInfoModle);

    }

    public void updataMachineId(String machineId) {
        clientInfoModle = getModleByDeviceNum();
        clientInfoModle.setMachineId(machineId);
        clientDao.update(clientInfoModle);

    }

    public void updataMachineNum(String machineNum) {
        clientInfoModle = getModleByDeviceNum();
        clientInfoModle.setMachineNum(machineNum);
        clientDao.update(clientInfoModle);

    }

    public void deleteAll(){
        clientDao.deleteAll();
    }
}
